package com.open.capacity.reimburse.service.impl;


import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.dao.SysFileinfoAuthorizeDao;
import com.open.capacity.reimburse.entity.SysFileinfoAuthorize;
import com.open.capacity.reimburse.service.SysFileinfoAuthorizeService;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Slf4j
@Service
public class SysFileinfoAuthorizeServiceImpl implements SysFileinfoAuthorizeService {

    @Autowired
    private SysFileinfoAuthorizeDao fileinfoAuthorizeDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysFileinfoAuthorize authorize) {
        if (StringUtils.isEmpty(authorize.getAuthType())) {
            authorize.setAuthType("1");//默认添加查看权限
        }
        fileinfoAuthorizeDao.save(authorize);
        log.info("保存文件共享授权：{}", authorize);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SysFileinfoAuthorize confluence) {

        fileinfoAuthorizeDao.updateByOps(confluence);
        log.info("修改文件共享授权：{}", confluence);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        SysFileinfoAuthorize confluence = fileinfoAuthorizeDao.findById(id);

        fileinfoAuthorizeDao.delete(id);

        log.info("删除文件共享授权：{}", confluence);
    }


    @Override
    public SysFileinfoAuthorize findById(String id) {
        return fileinfoAuthorizeDao.findById(id);
    }

    @Override
    public List<SysFileinfoAuthorize> findByFileinfoId(String fileinfoId) {
        return fileinfoAuthorizeDao.findByFileinfoId(fileinfoId);
    }


    @Override
    public Result saveOrUpdate(SysFileinfoAuthorize fileinfoAuthorize, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            SysFileinfoAuthorize model = fileinfoAuthorizeDao.findById(keyValue);
            if (model != null) {
                fileinfoAuthorize.setId(keyValue);
                i = fileinfoAuthorizeDao.updateByOps(fileinfoAuthorize);
            }
        } else {
            fileinfoAuthorize.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            i = fileinfoAuthorizeDao.save(fileinfoAuthorize);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
